package com.lap.framework.resources.utils;

import com.lap.framework.domain.Resources;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;

import java.lang.reflect.Method;

/**
 * <pre>
 *  Http 方法枚举
 * </pre>
 *
 * @author Shuisheng.Lao
 * @version 1.0.0
 */
public final class MethodEnum {
    /**
     * <pre>
     * 方法类型
     * </pre>
     *
     * @author Shuisheng.Lao
     * @version 1.0.0
     */
    public enum MappingMethod implements Operation {
        /**
         * Get
         */
        Get {
            @Override
            public Resources op(Method method, Resources parent) {
                GetMapping mapping = method.getAnnotation(GetMapping.class);
                String type = "GET";
                return getResources(parent, type, mapping.value());
            }
        },
        /**
         * Put
         */
        Put {
            @Override
            public Resources op(Method method, Resources parent) {
                PutMapping mapping = method.getAnnotation(PutMapping.class);
                String type = "PUT";
                return getResources(parent, type, mapping.value());
            }
        },
        /**
         * Post
         */
        Post {
            @Override
            public Resources op(Method method, Resources parent) {
                PostMapping mapping = method.getAnnotation(PostMapping.class);
                String type = "POST";
                return getResources(parent, type, mapping.value());
            }
        },
        /**
         * Delete
         */
        Delete {
            @Override
            public Resources op(Method method, Resources parent) {
                DeleteMapping mapping = method.getAnnotation(DeleteMapping.class);
                String type = "DELETE";
                return getResources(parent, type, mapping.value());
            }
        };

        /**
         * 初始化资源
         *
         * @param parent 父资源
         * @param type   方法类型
         * @param value  映射值
         * @return
         */
        private static Resources getResources(Resources parent, String type, String[] value) {
            Resources son = new Resources();
            son.setMethod(type);
            son.setParentUriCode(parent.getUriCode());
            son.setUri(parent.getUri() + UriUtils.getUri(value));
            son.setUriCode(UriUtils.getUriCode(parent.getModule(), son.getUri(), type.toLowerCase()));
            son.setDataFlag(Constants.DATA_FLAG);
            son.setLeafFlag(Constants.IS_LEAF);
            son.setTypeFlag(Constants.SERVICE_FLAG);
            return son;
        }

        /**
         * <pre>
         * 根据方法和父资源获取资源信息
         * </pre>
         *
         * @param method 方法
         * @param parent 父资源
         * @return 资源信息
         */
        public static Resources getResource(Method method, Resources parent) {
            if (method.isAnnotationPresent(GetMapping.class)) {
                return Get.op(method, parent);
            } else if (method.isAnnotationPresent(PutMapping.class)) {
                return Put.op(method, parent);
            }
            if (method.isAnnotationPresent(PostMapping.class)) {
                return Post.op(method, parent);
            }
            if (method.isAnnotationPresent(DeleteMapping.class)) {
                return Delete.op(method, parent);
            } else {
                return null;
            }
        }
    }

    /**
     * <pre>
     * 操作判断枚举
     * </pre>
     *
     * @author Shuisheng.Lao
     * @version 1.0.0
     */
    interface Operation {
        /**
         * <pre>
         *  初始化方法
         * </pre>
         *
         * @param method 方法
         * @param parent 父资源
         * @return 子资源
         */
        Resources op(Method method, Resources parent);
    }

}
